<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>compile编译节点</title>
</head>
<body>
    <div id="app">
        <input type="text" id="a" v-model="text">
        {{ text }}
    </div>
    <script>
        /* vue的使用方式
        var vm = new Vue({
            el:'app',
            data:{
                text:'hello word'
            }
        })
        */

        var obj = {
            text:'hello word'
        };

        function compile(node,obj){
            var type = node.nodeType;
            //1为元素节点 3为文本节点
            if(type==1){
                //去属性集合
                var attrs = node.attributes;
                for (let index = 0; index < attrs.length; index++) {
                    if(attrs[index].nodeName=="v-model"){
                        var name = attrs[index].nodeValue;
                        node.value = obj[name];
                    } 
                }
            }else if(type==3){
                //匹配获取name
                var reg = /\{\{(.*)\}\}/;
                if(reg.test(node.nodeValue)){
                    var name = RegExp.$1.trim();
                    node.nodeValue = obj[name];
                }
            }
        }
        var input = document.querySelector('#a');
        var text = document.querySelector('#app').lastChild;
        compile(input,obj);
        compile(text,obj);
    </script>
</body>
</html>